<template>
  <div>
    <!-- 添加管理员弹框 -->
    <el-dialog
      title="添加管理员"
      :visible="open"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="630px"
      append-to-body
      @close="cancel()"
    >
      <el-form
        v-loading="loading"
        @submit.native.prevent
        ref="form"
        :rules="rules"
        :model="form"
        label-width="110px"
      >
        <el-form-item v-if="shopObject.regionName" label="大区">
          {{ shopObject.regionName }}
        </el-form-item>
        <el-form-item label="门店">
          {{ shopObject.schoolName }}
        </el-form-item>

        <el-form-item label="用户头像" prop="avatar" v-if="form.userId">
          <img
            v-bind:src="
              form.avatar
                ? form.avatar
                : require('@/assets/images/head_img.png')
            "
            class="img-circle img-lg"
          />
        </el-form-item>
        <el-form-item label="账号(手机号)" prop="studentId">
          <el-select
            v-model="form.studentId"
            placeholder="请选择用户"
            filterable
            clearable
            @change="changeUser"
          >
            <el-option
              v-for="dict in user"
              :key="dict.studentId"
              :label="`${dict.name}(${dict.phoneNumber})`"
              :value="dict.studentId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="姓名" prop="name" v-if="form.userId">
          <div>{{ form.name }}</div>
        </el-form-item>
        <el-form-item label="用户性别" v-if="form.userId">
          <div>{{ form.sex == 0 ? "男" : form.sex == 1 ? "女" : "" }}</div>
        </el-form-item>
        <el-form-item label="所属岗位" prop="postIds" v-if="form.userId">
          <div style="overflow-y: scroll; max-height: calc(85vh - 600px)">
            <el-card
              style="width: 95%"
              class="box-card"
              v-for="(item, index) in form.regionSchoolPostNames"
              :key="index"
              >{{ item }}
            </el-card>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-preventReClick @click="cancel">取 消</el-button>
        <el-button v-preventReClick type="primary" @click="submitForm"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
    
<script>
import {
  getPost_student,
  listPost_student,
  setSchoolAdmin,
} from "@/api/schoolOrganization/student";

export default {
  props: {
    open: Boolean,
    // 当type==shopAdmin 添加门店管理员时候 则需要传入门店id
    shopObject: {
      require: false,
      type: [Object],
    },
    existsAdmins: Array,
  },
  data() {
    return {
      // 表单参数
      form: {},
      loading: false,
      user: [],
      rules: {
        studentId: [
          { required: true, message: "员工不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    async open(e) {
      if (e) {
        this.form = {};
        this.resetForm("form");
        try {
          this.loading = true;
          let res = await listPost_student({
            pageNum: 1,
            pageSize: 9999,
            status: 0,
            schoolIds: this.shopObject.schoolId,
          });
          this.user = res.rows.filter((item) => {
            return !this.existsAdmins
              .map((item) => {
                return item.userId;
              })
              .includes(item.userId);
          });
        } finally {
          this.loading = false;
        }
      }
    },
  },
  mounted() {},
  methods: {
    /* 获取用户信息 */
    async changeUser(e) {
      try {
        this.loading = true;
        let res = await getPost_student(e);
        this.form = res.data;
      } finally {
        this.loading = false;
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          try {
            this.loading = true;
            await setSchoolAdmin({
              userId: this.form.userId,
              schoolId: this.shopObject.schoolId,
            });
            this.$modal.msgSuccess("设置管理员成功");
            this.$emit("update:open", false);
            this.$emit("refresh");
          } finally {
            this.loading = false;
          }
        }
      });
    },

    // 表单重置
    cancel() {
      this.$emit("update:open", false);
    },
  },
};
</script>
<style scoped lang="scss">
::v-deep .el-card__body {
  padding: 0px 20px 0px 20px;
}
.box-card {
  margin-top: 2px;
}
.box-card:nth-of-type(1) {
  margin-top: 0px;
}
::v-deep .el-card__header {
  padding: 0 20px;
}
</style>
    